<?php
/**
 * Created by PhpStorm.
 * Author: 黑瞎子
 * Date: 2018/3/19
 * Time: 14:59
 */

namespace app\api\controller\v1;


use app\api\controller\BaseCotroller;
use app\api\validate\IDMustBePostiveInt;
use app\api\validate\OrderPlace;
use app\api\validate\PagingParameter;
use app\lib\exception\OrderException;
use app\lib\exception\SuccessMessage;
use think\Controller;
use app\api\service\Token as TokenService;
use app\api\service\Order as OrderService;
use app\api\model\Order as OrderModel;

class Order extends BaseCotroller
{
    //前置操作-用户权限验证
    protected $beforeActionList = [
        'checkExclusiveScope' => ['only' => 'placeOrder'],
        'checkPrimaryScope' => ['only' => 'getSummaryByUser,getDetail']
    ];

    /*
     * 订单接口
     * 用户在选择好商品后，向API提交包含它所选择商品的相关信息
     * API在接受到信息后，需要检查订单相关商品的库存量
     * 有库存则把订单数据存入数据库中，这一步相当于下单成功，然后返回客户端消息，通知客户端可以支付了
     * 调用我们的支付接口，进行支付，支付前还需再次进行库存量检测
     * 服务器这边就可以调用微信的支付接口进行支付
     * 小程序根据服务器返回的结果拉起微信支付
     * 微信会返回给我们一个支付的结果（异步）
     * 支付成功：也需要进行库存量的检查
     * 支付成功：进行库存量的扣除
     * */
    //生成订单
    public function placeOrder(){
        (new OrderPlace())->goCheck();
        $products = input('post.products/a');
        $uid = TokenService::getCurrentUid();

        $order = new OrderService();
        $status = $order->place($uid, $products);
        return $status;
    }

    //用户历史订单查询
    //page 页数  size 每页条数
    public function getSummaryByUser($page = 1, $size = 15){
        (new PagingParameter())->goCheck();
        $uid = TokenService::getCurrentTokenVar('uid');
        $pagingOrders = OrderModel::getSummaryByUser($uid, $page, $size);
        if ($pagingOrders->isEmpty()){
            return [
                'date' => [],
                'current_page' => $pagingOrders->getCurrentPage(),
            ];
        }
//        $date = $pagingOrders->hidden(['prepay_id'])->toArray();
        return [
            'data' => $pagingOrders->hidden(['snap_items', 'snap_address', 'prepay_id']),
            'current_page' => $pagingOrders->getCurrentPage(),
        ];
    }

    /* CMS
     * 获取所有订单简要信息
     * @params: $page int 第几页
     *          $size int 每页条数
     * @return array
     * */
    public function getSummary($page = 1, $size = 20){
        (new PagingParameter())->goCheck();
        $pagingOrders = OrderModel::getSummaryByPage($page, $size);
        if ($pagingOrders->isEmpty()){
            return [
                'date' => [],
                'current_page' => $pagingOrders->getCurrentPage(),
            ];
        }
//        $date = $pagingOrders->hidden(['prepay_id'])->toArray();
        return [
            'data' => $pagingOrders->hidden(['snap_items', 'snap_address']),
            'current_page' => $pagingOrders->getCurrentPage(),
        ];
    }

    //订单详情信息
    public function getDetail($id){
        (new IDMustBePostiveInt())->goCheck();
        $orderDetail = OrderModel::get($id);
        if (!$orderDetail){
            throw new OrderException();
        }
        return $orderDetail->hidden(['prepay_id']);
    }

    // CMS发送模板消息 -发货
    // $id 订单id
    public function delivery($id){
        (new IDMustBePostiveInt())->goCheck();
        $order = new OrderService();
        $success = $order->delivery($id);
        if ($success){
            new SuccessMessage();
        }
    }
}